package service

import (
	"net/http"

	"gitee.com/zenpk/backend-demo/internal/dal"
	"gitee.com/zenpk/backend-demo/internal/dto"
	"gitee.com/zenpk/backend-demo/internal/middleware"
	"github.com/gin-gonic/gin"
)

type ConsultSituation struct{}

var consultStudentInfoDal dal.ConsultStudentInfo
var consultSituationDal dal.ConsultSituation
var fileInfoDal dal.FileInfo

func (s *ConsultSituation) AddNext(c *gin.Context) {
	var req dto.AddNextReq
	if err := c.ShouldBindJSON(&req); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.AddNextResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	_, err := consultStudentInfoDal.GetByStuNum(req.StudentNum)
	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.AddNextResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	//分别插入consult_situations表和fileinfos表
	var consultSituation dal.ConsultSituation
	var fileInfo dal.FileInfo
	consultSituation.StudentNum = req.StudentNum
	consultSituation.MainComplaint = req.MainComplaint
	consultSituation.Problem = req.Problem
	consultSituation.Experience = req.Experience
	consultSituation.FamilyHistory = req.FamilyHistory
	consultSituation.MentalState = req.MentalState
	consultSituation.DangerEstimate = req.DangerEstimate
	consultSituation.ConsultProblem = req.ConsultProblem
	consultSituation.CrisisInterfere = req.CrisisInterfere
	consultSituation.MentalObstacle = req.MentalObstacle
	consultSituation.ConsultantName = req.ConsultantName
	consultSituation.StartTime = req.StartTime
	consultSituation.EndTime = req.EndTime
	consultSituation.ConsultContent = req.ConsultContent
	consultSituation.HandleAdvice = req.HandleAdvice
	consultSituation, err = consultSituationDal.Create(&consultSituation)
	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.AddNextResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	recordid := consultSituation.Recordid
	fileInfo.Recordid = recordid
	fileInfo.Filename = req.Filename
	fileInfo.Fileformat = req.Fileformat
	fileInfo.Filesize = req.Filesize
	fileInfo.Uploadtime = req.Uploadtime
	if err = fileInfoDal.AddFile(&fileInfo); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.AddNextResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, dto.AddNextResp{
		SuccessCode: 1,
		Msg:         "添加成功",
	})
}

func (s *ConsultSituation) GetStudentConsultRecord(c *gin.Context) {
	var req dto.GetStudentConsultRecordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.AddNextResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}

	stuConsultRecord, err := consultSituationDal.GetStudentConsultRecord(req.Page, req.Pagesize)
	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.GetStudentConsultRecordResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, dto.GetStudentConsultRecordResp{
		SuccessCode: 1,
		Msg:         "获取咨询学生表单数据成功",
		Data:        stuConsultRecord,
	})
	return
}

func (s *ConsultSituation) DeleteStudentConsultRecord(c *gin.Context) {
	var req dto.DeleteStudentConsultRecordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.DeleteStudentConsultRecordResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	err := consultSituationDal.DeleteStudentConsultRecord(req.RecordID)
	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.DeleteStudentConsultRecordResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, dto.DeleteStudentConsultRecordResp{
		SuccessCode: 1,
		Msg:         "删除咨询学生表单数据成功",
	})
}

func (s *ConsultSituation) EditStudentConsultRecord(c *gin.Context) {
	var req dto.EditStudentConsultRecordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.EditStudentConsultRecordResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	var updateRecord dal.StudentConsultRecord
	updateRecord.Recordid = req.Recordid
	updateRecord.StudentNum = req.StudentNum
	updateRecord.Name = req.Name
	updateRecord.Gender = req.Gender
	updateRecord.Organization = req.Organization
	updateRecord.StartTime = req.Consulttime
	updateRecord.HandleAdvice = req.Handleadvice
	updateRecord.ConsultState = req.Ended
	updateRecord.EndTime = req.Committime
	updateRecord.ConsultTime = req.Consultans
	err := consultSituationDal.EditStudentConsultRecord(&updateRecord)
	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.EditStudentConsultRecordResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, dto.EditStudentConsultRecordResp{
		SuccessCode: 1,
		Msg:         "编辑咨询学生表单数据成功",
	})
}

func (s *ConsultSituation) AddStudentConsultRecord(c *gin.Context) {
	var req dto.AddStudentConsultRecordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.AddStudentConsultRecordResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	var addRecord dal.StudentConsultRecord
	addRecord.Recordid = req.Recordid
	addRecord.StudentNum = req.StudentNum
	addRecord.Name = req.Name
	addRecord.Gender = req.Gender
	addRecord.Organization = req.Organization
	addRecord.StartTime = req.Consulttime
	addRecord.HandleAdvice = req.Handleadvice
	addRecord.ConsultState = req.Ended
	addRecord.EndTime = req.Committime
	addRecord.ConsultTime = req.Consultans
	err := consultSituationDal.AddStudentConsultRecord(&addRecord)
	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.AddStudentConsultRecordResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, dto.AddStudentConsultRecordResp{
		SuccessCode: 1,
		Msg:         "添加咨询学生表单数据成功",
	})
}

func (s *ConsultSituation) GetStudentConsultRecordByPara(c *gin.Context) {
	var req dto.GetStudentConsultRecordByParaReq
	if err := c.ShouldBindJSON(&req); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.GetStudentConsultRecordByParaResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	var CheckVar dal.StudentConsultRecord
	CheckVar.StudentNum = req.StudentNum
	CheckVar.Name = req.Name
	CheckVar.Gender = req.Gender
	CheckVar.Organization = req.Organization
	CheckVar.HandleAdvice = req.Handleadvice
	CheckVar.ConsultState = req.Ended
	if req.ParaArray[6] == 1 {
		i := 0
		n := len(req.DateScale)
		for i = 0; i < len(req.DateScale); i++ {
			if req.DateScale[i] == '~' {
				break
			}
		}
		CheckVar.StartTime = req.DateScale[0:i]
		CheckVar.EndTime = req.DateScale[i+1 : n]
	}
	page := req.Page
	pagesize := req.Pagesize
	stuconsultRecord, err := consultSituationDal.GetStudentConsultRecordByPara(req.ParaArray, CheckVar, page, pagesize)
	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.GetStudentConsultRecordByParaResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, dto.GetStudentConsultRecordByParaResp{
		SuccessCode: 1,
		Msg:         "获取咨询学生表单数据成功",
		Data:        stuconsultRecord,
	})
	return
}

// ReopenConsultSituation 重新开启单个咨询记录 (重启后对应的是编辑）
func (s *ConsultSituation) ReopenConsultSituation(c *gin.Context) {
	var req dto.DeleteStudentConsultRecordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.ReopenConsultSituationResp{
			SuccessCode: 0,
			State:       0,
			Msg:         err.Error(),
		})
		return
	}
	id := req.RecordID
	consultState, err := consultSituationDal.ReopenConsultSituation(id)
	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.ReopenConsultSituationResp{
			SuccessCode: 0,
			State:       consultState,
			Msg:         err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, dto.ReopenConsultSituationResp{
		SuccessCode: 1,
		State:       consultState,
		Msg:         "重新启动成功",
	})
}

func (s *ConsultSituation) GiveEnd(c *gin.Context) {
	var req dto.DeleteStudentConsultRecordReq
	if err := c.ShouldBindJSON(&req); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.GiveEndResp{
			SuccessCode: 0,
			State:       0,
			Msg:         err.Error(),
		})
		return
	}
	id := req.RecordID
	consultState, err := consultSituationDal.GiveEnd(id)
	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusBadRequest, dto.GiveEndResp{
			SuccessCode: 0,
			State:       consultState,
			Msg:         err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, dto.GiveEndResp{
		SuccessCode: 1,
		State:       consultState,
		Msg:         "结案成功",
	})
	return
}

func (s *ConsultSituation) GetConsultSituation(c *gin.Context) {
	var consultSituation []dal.ConsultSituation
	var req dto.GetConsultSituationReq
	//请求绑定json
	if err := c.ShouldBindJSON(&req); err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusOK, dto.GetConsultSituationResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}
	consultSituation, err := consultSituationDal.GetConsultSituation(req.StudentNum)

	if err != nil {
		middleware.Logger.Warn(err.Error())
		c.JSON(http.StatusOK, dto.GetConsultSituationResp{
			SuccessCode: 0,
			Msg:         err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, dto.GetConsultSituationResp{
		SuccessCode:    1,
		Msg:            "获取成功",
		StudentNum:     req.StudentNum,
		SituationArray: consultSituation,
	})
}
